soda()
	._page('iam_module')
	._api('/v1/iam/module')
	._query(['name^like^', 'state^eq^-1'])
	._orderBy(['sortId^desc'])
	._run((unit) => {
		// 表单配置
		unit.templateForm['verify'] = ["name", "code"];
		unit.templateForm['items'] = [
			{code: 1, name:'name', title:'模块名称', placeholder:'请输入模块名称', attr: 'maxlength=20'},
			{code: 1, name:'code', title:'模块编码', placeholder:'请输入模块编码', attr: 'maxlength=20'},
			{code: 1, name:'sortId', title:'排序', type: 'number'},
			{code: 2, name:'state', title:'状态', options: unit.globalConstant['enableState']},
			{code: 5, type:'text', name:'remark', title:'备注', placeholder:'备注信息'}
		];
		
		// 表格基础配置
		unit.templateTable['cols'] = [
			{fixed: 'left', field: 'name', title: '模块名称', minWidth: 120},
			{fixed: 'left', field: 'code', title: '模块编码', minWidth: 120},
			{field: 'sortId', title: '排序'},
			{field: 'state', title: '状态',templet(obj) {
			    return unit.sdFormat['json']['get'](unit.globalConstant['enableState'], obj.state, 'value').title;
			}},
			{field: 'createTime', title: '创建时间'},
			{field: 'updateTime', title: '上次修改', templet(obj) {
			    let ct = obj.createTime;
			    let ut = obj.updateTime;
			    return ct == ut ? '' : ut;
			}},
			{fixed: 'right', title: '操作', minWidth: 120, toolbar: unit.sysApplication['btns']['right']()},
		];
		unit.templateTable['bar'] = (bar) => {
			unit.templateDialog[bar.event]();
		}
		unit.templateTable['tool'] = (tool) => {
			unit.templateDialog[tool.event](tool.data);
		}
		// 表格查询配置
		unit.templateTable['search']['components'] = [
			{code:1, name:"name", title:"应用名称", placeholder:"请输入要查询的关键字"},
			{code:3, name:"state", title:"状态", options: unit.globalConstant['enableState']}
		]
		unit.templateTable['search']['submit'] = (data) => {
			unit.sdTable.reload(unit.el['table'], {
				where: unit.sdFormat['query'](unit.filter['vo'], data)
			});
		}
		
		//弹出层
		unit.templateDialog['form'] = (data) => {
			unit.sdDialog.form({
				title: data ? '修改' : "创建",
				height: '500px',
				init() {
					unit.sdForm['init']['form'](unit.templateForm, data);
				},
				opt(index) {
					// 获取form
					let form = unit.sdForm['get'](unit.el['form'], unit.templateForm['verify']);
					
					// 校验数据
					if (!form.check) {
						unit.sdDialog['tips']['warning'](form.message);
						return false;
					}
					
					// 数据处理
					let val = form.data;
					val['state'] = val['state'] || 1;
			
					let param = {
						url: unit.api['crud'],
						data: val,
						tips: true,
						callback() {
							unit.sdDialog['close'](index);
							unit.sdTable['reload'](unit.el.table);
						}
					}
					// 操作
					data ? unit.sdHttp['put'](param, data.id) : unit.sdHttp['post'](param);
				}
			})
		}
		unit.templateDialog['create'] = unit.templateDialog['form'];
		unit.templateDialog['update'] = unit.templateDialog['form'];
		unit.templateDialog['delete'] = (data) => {
			unit.sdDialog.confirm({
				title: "是否要删除这条数据?",
				submit() {
					let param = {
						url: unit.api['crud'],
						callback() {
							unit.sdTable['reload'](unit.el.table)
						}
					}
					unit.sdHttp['delete'](param, data.id);
				}
			});
		}
		
		unit.sdTable['render'](unit.templateTable);
	});
